﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CrystalDecisions.CrystalReports.Engine;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmRefundFee : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        string companyID = GlobalVariable.WSInfo.CompanyID;
        string _makhoahoc;
        string _userid = GlobalVariable.WSInfo.UserID;
        int _id;

        public frmRefundFee()
        {
            InitializeComponent();
        }

        private void frmRefundFee_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void searchLookUpEdit_KhoaHoc_EditValueChanged(object sender, EventArgs e)
        {
            _makhoahoc = searchLookUpEdit_KhoaHoc.EditValue.ToString();
            LoadHocVienByKhoaHoc(_makhoahoc);
        }

        private void searchLookUpEdit_HocVien_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                string invoicenumber = searchLookUpEdit_HocVien.EditValue.ToString();
                LoadEnquiryByInvoiceNumber(invoicenumber);
                LoadInvoiceByInvoiceNumber(invoicenumber);
                lblInvoiceNumber.Text = invoicenumber;
                lblEnquiryNumber.Text = ds.Tables["TableEnquiryByInvoiceNumber"].Rows[0][0].ToString();
                lblHoTen.Text = searchLookUpEdit_HocVien.Text;
                dateEdit_NgayRefund.EditValue = DateTime.Now.ToShortDateString();
                lblSoTienDaDong.Text = String.Format("{0:N0}", ds.Tables["TableInvoiceByInvoiceNumber"].Rows[0]["Amount"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_DataOfRefund_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _id = Convert.ToInt32(gridView_DataOfRefund.GetRowCellValue(e.FocusedRowHandle, "ID"));
        }

        //button-----------------------------------------------------------------------------

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation())
            {
                //--
                string invoice_number = lblInvoiceNumber.Text;
                string enquiry_number = lblEnquiryNumber.Text;
                DateTime ngay = Convert.ToDateTime(dateEdit_NgayRefund.EditValue);
                string lydo = memoEdit1.Text;
                double sotien_refund = Convert.ToDouble(lblSoTienDaDong.Text);
                string user_id = _userid;
                string makhoahoc = searchLookUpEdit_KhoaHoc.EditValue.ToString();
                //--
                DongYRefund(invoice_number, enquiry_number, makhoahoc, ngay, lydo, user_id, sotien_refund);
                //--Load lai danh sach refund
                LoadRefundFee();
                gridControl_DataOfRefund.DataSource = ds.Tables["TableRefundFee"];
                /*
                //--Delete InvoiceKhoaHoc
                DeleteInvoiceKhoaHocByInvoiceNumberAndMaKhoaHoc(invoice_number, makhoahoc);
                //--Delete InvoiceDetail
                DeleteInvoiceDetail(invoice_number);
                //--Delete Invoice
                DeleteInvoice(invoice_number);
                //--Load lai danh sach refund
                LoadRefundFee();
                gridControl_DataOfRefund.DataSource = ds.Tables["TableRefundFee"];
                */
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                PrintData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadData()
        {
            LoadKhoaHoc();
            LoadRefundFee();
            gridControl_DataOfRefund.DataSource = ds.Tables["TableRefundFee"];
        }

        private void LoadRefundFee()
        {
            try
            {
                if (ds.Tables["TableRefundFee"] != null)
                    ds.Tables["TableRefundFee"].Clear();
                string sql = @"usp_LoadRefundFee";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableRefundFee");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHoc()
        {
            try
            {
                if (ds.Tables["TableKhoaHoc"] != null)
                    ds.Tables["TableKhoaHoc"].Clear();
                string sql = @"SELECT MaKhoaHoc, TenKhoaHoc FROM DM_KHOAHOC WHERE TrangThai NOT IN ('End')
                            AND MaDonVi = '" + companyID + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHoc");
                searchLookUpEdit_KhoaHoc.Properties.DataSource = ds.Tables["TableKhoaHoc"];
                searchLookUpEdit_KhoaHoc.Properties.DisplayMember = "TenKhoaHoc";
                searchLookUpEdit_KhoaHoc.Properties.ValueMember = "MaKhoaHoc";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadHocVienByKhoaHoc(string makhoahoc)
        {
            try
            {
                if (ds.Tables["TableInvoiceByKhoaHoc"] != null)
                    ds.Tables["TableInvoiceByKhoaHoc"].Clear();
                string sql = @"usp_LoadInvoiceByKhoaHoc @makhoahoc = '" + makhoahoc + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceByKhoaHoc");
                searchLookUpEdit_HocVien.Properties.DataSource = ds.Tables["TableInvoiceByKhoaHoc"];
                searchLookUpEdit_HocVien.Properties.ValueMember = "InvoiceNumber";
                searchLookUpEdit_HocVien.Properties.DisplayMember = "HoTen";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadEnquiryByInvoiceNumber(string invoice_number)
        {
            try
            {
                if (ds.Tables["TableEnquiryByInvoiceNumber"] != null)
                    ds.Tables["TableEnquiryByInvoiceNumber"].Clear();
                string sql = @"usp_LoadEnquiryByInvoiceNumber @invoice_number = '" + invoice_number + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableEnquiryByInvoiceNumber");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadInvoiceByInvoiceNumber(string invoice_number)
        {
            try
            {
                if (ds.Tables["TableInvoiceByInvoiceNumber"] != null)
                    ds.Tables["TableInvoiceByInvoiceNumber"].Clear();
                string sql = @"usp_LoadInvoiceByInvoiceNumber @invoice_number = '" + invoice_number + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceByInvoiceNumber");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /*
        private void LoadInvoiceKhoaHocByInvoiceNumberAndMaKhoaHoc(string invoice_number, string makhoahoc)
        {
            try
            {
                if (ds.Tables["TableInvoiceKhoaHocByInvoiceNumberAndMaKhoaHoc"] != null)
                    ds.Tables["TableInvoiceKhoaHocByInvoiceNumberAndMaKhoaHoc"].Clear();
                string sql = @"usp_LoadInvoiceKhoaHocByInvoiceNumerAndMaKhoaHoc @invoice_number = '" + invoice_number + @"'
                                , @makhoahoc = '" + makhoahoc + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceKhoaHocByInvoiceNumberAndMaKhoaHoc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        */

        private void DongYRefund(string invoice_number, string enquiry_number, string makhoahoc, DateTime ngay, 
                                    string lydo, string user_id, double sotien_refund)
        {
            try
            {
                string sql = @"usp_InsertRefundFee @invoice_number = @inv_num, @enquiry_number = @enq_num, @makhoahoc = @mkh,
                                @ngay = @date, @lydo = @reason, @userid = @user_id, @sotien_refund = @sotienrefund";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@inv_num", invoice_number);
                cmd.Parameters.AddWithValue("@enq_num", enquiry_number);
                cmd.Parameters.AddWithValue("@mkh", makhoahoc);
                cmd.Parameters.AddWithValue("@date", ngay);
                cmd.Parameters.AddWithValue("@reason", lydo);
                cmd.Parameters.AddWithValue("@sotienrefund", sotien_refund);
                cmd.Parameters.AddWithValue("@user_id", user_id);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Đồng ý refund thành công!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /*
        private void DeleteInvoice(string invoice_number)
        {
            try
            {
                string sql = @"usp_DeleteInvoice @invoice_number = @inv_num";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@inv_num", invoice_number);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Đã xóa Invoice");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DeleteInvoiceDetail(string invoice_number)
        {
            try
            {
                string sql = @"usp_DeleteInvoiceDetail @invoice_number = @inv_num";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@inv_num", invoice_number);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Đã xóa InvoiceDetail");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DeleteInvoiceKhoaHocByInvoiceNumberAndMaKhoaHoc(string invoice_number, string makhoahoc)
        {
            try
            {
                string sql = @"usp_DeleteInvoiceKhoaHocByInvoiceNumberAndMaKhoaHoc @invoice_number = @inv_num, @makhoahoc = @mkh";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@inv_num", invoice_number);
                cmd.Parameters.AddWithValue("@mkh", makhoahoc);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Ko còn trong lớp");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        */

        private void PrintData()
        {
            try
            {
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Application.StartupPath + "\\Report\\myrpt_RefundFee.rpt");
                //Set connection
                PublicFunction.SetDBLogonForReport(rpt);
                //Set parameter
                PublicFunction.SetParameter(rpt, "@id", _id);
                //View report
                frmPrintPreview review = new frmPrintPreview(rpt);
                review.ShowDialog();
                review.Dispose();
                rpt.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool Validation()
        {
            try
            {
                if (lblInvoiceNumber.Text == "" || lblInvoiceNumber.Text == "- - -")
                {
                    MessageBox.Show("Bạn chưa có mã số học viên");
                    return false;
                }
                if (lblEnquiryNumber.Text == "" || lblEnquiryNumber.Text == "- - -")
                {
                    MessageBox.Show("Bạn chưa có mã tư vấn");
                    return false;
                }
                if (lblHoTen.Text == "" || lblHoTen.Text == "- - -")
                {
                    MessageBox.Show("Bạn chưa có họ tên học viên");
                    return false;
                }
                if (memoEdit1.Text == "")
                {
                    MessageBox.Show("Bạn chưa có lý do refund học phí");
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        
        #endregion
    }
}
